package cn.getech.data.development.controller;

import cn.getech.data.development.entity.BdpDataCategory;
import cn.getech.data.development.model.dto.ProjectListDTO;
import cn.getech.data.development.model.dto.real.RealTableQueryDTO;
import cn.getech.data.development.model.dto.real.RealTableQueryReturnDTO;
import cn.getech.data.development.model.vo.BdpDataCategoryTableInfoVO;
import cn.getech.data.development.service.BdpDataCategoryService;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 类目信息表Controller
 *
 * @author bianj
 * @version 1.0.0 2020-09-29
 */
@Api(tags = "类目信息表服务")
@RestController
@RequestMapping("/bdpDataCategory")
public class BdpDataCategoryController {

    @Autowired
    private BdpDataCategoryService bdpDataCategoryService;


    @ApiOperation(value = "查询类目信息")
    @GetMapping("/list")
    //@SysLog(value = "查询数据类目信息", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R list() {
        List<BdpDataCategory> bdpDataCategoryByCondition = bdpDataCategoryService.findBdpDataCategoryByCondition();
        return R.okWithData(bdpDataCategoryByCondition);
    }

    @ApiOperation(value = "类目名称模糊查询")
    @ApiImplicitParam(name = "params", value = "类目名称", required = true)
    @GetMapping(value = "/getCategoryName")
    //@SysLog(value = "模糊搜索数据类目信息", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R getCategory(@RequestParam String params) {
        if (params == null) {
            return R.error("请选择需要查询的数据！");
        }
        return R.okWithData(bdpDataCategoryService.getCategoryName(params));
    }

    @ApiOperation(value = "修改类目信息表信息")
    @PutMapping(value = "/update")
    @SysLog(value = "修改类目信息", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.TABLEMODELCODE, platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R update(@RequestBody BdpDataCategory bdpDataCategory) {
        if (bdpDataCategory == null || bdpDataCategory.getId() == null || bdpDataCategory.getCategoryName() == null || bdpDataCategory.getLevel() == null) {
            return R.error("请选择需要修改的数据！");
        }
        /**更新不能重命名*/
        boolean bool = bdpDataCategoryService.updateBdpDataCategory(bdpDataCategory);
        if (bool) {
            return R.ok();
        }
        return R.error("同一级目录下不能重名");
    }

    @ApiOperation(value = "根据主键ID删除类目信息表")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
    @DeleteMapping(value = "/delete/{id}")
    @SysLog(value = "删除类目信息", type = OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.TABLEMODELCODE, platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R delete(@PathVariable Integer id) {
        if (id == null) {
            return R.error("请选择需要删除的数据！");
        }
        int bool = bdpDataCategoryService.deleteBdpDataCategoryById(id);
        if (3 == bool) {
            return R.ok();
        }
        if (2 == bool) {
            return R.error("此目录下存在子级不允许删除");
        }
        if (1 == bool) {
            return R.error("此目录下存在数据不允许删除");
        }
        return R.error("错误");
    }


    @ApiOperation(value = "新增类目信息信息")
    @PostMapping("/add")
    @SysLog(value = "新增数据类目", type = OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.TABLEMODELCODE, platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R add(@RequestBody BdpDataCategory bdpDataCategory) {
        if (bdpDataCategory.getCategoryName() == null) {
            return R.error("请添加类目数据！");
        }
        if (bdpDataCategory.getCategoryName().length() > 50 || bdpDataCategory.getCategoryName().length() < 2) {
            return R.error("类目参数错误！");
        }
        boolean bool = bdpDataCategoryService.addBdpDataCategory(bdpDataCategory);
        if (bool) {
            return R.ok();
        }
        return R.error("同一级目录下不能重名");
    }


    @ApiOperation(value = "根据类目ID查询类目信息下表的信息   分页多条件查询")
    @GetMapping(value = "/get")
    //@SysLog(value = "查询类目下的表数据", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R get(@RequestParam Map<String, Object> params) {
        PageUtils bdpDataCategoryById = bdpDataCategoryService.getBdpDataCategoryById(params);
        return R.okWithData(bdpDataCategoryById);
    }

    @ApiOperation(value = "数据类目 项目列表查询")
    @GetMapping(value = "/project/list")
    public R projectList() {
        Set<ProjectListDTO> proNames = bdpDataCategoryService.projectList();
        return R.okWithData(proNames);
    }
    @ApiOperation(value = "表的创建人")
    @GetMapping("/tableCreateName")
    public R getTableNameList(@RequestParam Map<String, Object> params) {
        List<String> tableInfoCrateName = bdpDataCategoryService.getTableInfoCrateName(params);
        return R.okWithData(tableInfoCrateName);
    }

    @ApiOperation(value = "查询不在该类目下的表信息   分页多条件查询（离线表）")
    @GetMapping(value = "/getTable")
    //@SysLog(value = "查询不在该类目下的表信息(离线表)", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R getTable(@RequestParam Map<String, Object> params) {
        PageUtils notBdpDataCategoryById = bdpDataCategoryService.getNotBdpDataCategoryById(params);
        return R.okWithData(notBdpDataCategoryById);
    }


    @ApiOperation(value = "查询不在该类目下的表信息  (实时表) 分页多条件查询")
    @PostMapping(value = "/getRealTable")
    @SysLog(value = "查询不在该类目下的表信息(实时表)", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE, platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R getRealTable(@RequestBody RealTableQueryDTO tableQueryDTO) {
        IPage<RealTableQueryReturnDTO> page = bdpDataCategoryService.getRealTable(tableQueryDTO);
        return R.okWithPage(page);
    }

    @ApiOperation(value = "判断类目下是否有表")
    @ApiImplicitParam(name = "id", value = "类目ID", required = true)
    @GetMapping(value = "/getTableTrue/{id}")
    //@SysLog(value = "判断类目下是否存在表", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.TABLEMODELCODE,platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R getTableTrue(@PathVariable String id) {
        if (id == null) {
            return R.error("数据不能为空");
        }
        if (isNumeric(id)) {
            boolean bool = bdpDataCategoryService.ifataCategoryBastInfo(id);
            if (bool) {
                return R.ok();
            }
            return R.error("类目下存在子目录或者数据表");
        }
        return R.error("请输入类目id");
    }


    @ApiOperation(value = "根据表id表批量添加到类目")
    @PutMapping("/saveList")
    @SysLog(value = "关联表到类目", type = OperationTypeEnum.RELATIONCODE, modulesType = OperationModulesEnum.TABLEMODELCODE, platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    public R saveList(@RequestBody BdpDataCategoryTableInfoVO idList) {
        if (idList.getCategoryIds() == null || idList.getCategoryIds().size() == 0 || idList.getTabinfoIds() == null || idList.getTabinfoIds().size() == 0) {
            return R.error("数据不能为空");
        }
        boolean bool = bdpDataCategoryService.saveBdpDataCategoryByIds(idList);
        if (bool) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "根据表id批量删除")
    @PostMapping(value = "/deleteList")
    @SysLog(value = "移除类目下的表", type = OperationTypeEnum.DISASSOCIATIONCODE, modulesType = OperationModulesEnum.TABLEMODELCODE, platformModuleType = ModuleEnum.DATA_MAP_MODULE_CODE)
    @ResponseBody
    public R deleteList(@RequestBody BdpDataCategoryTableInfoVO idList) {
        if (idList.getId() == null || idList.getTabinfoIds() == null || idList.getTabinfoIds().size() == 0) {
            return R.error("数据不能为空");
        }
        boolean bool = bdpDataCategoryService.deleteBdpDataCategoryByIds(idList);
        if (bool) {
            return R.ok();
        }
        return R.error();
    }


    @ApiOperation(value = "查询数据库")
    @GetMapping("/DBTableNameList")
    public R getDBTableNameList(@RequestParam Map<String, Object> params) {
        List<String> tableInfoCrateName = bdpDataCategoryService.getDBTableList(params);
        return R.okWithData(tableInfoCrateName);
    }

    @ApiOperation(value = "项目列表")
    @GetMapping("/procInfoName")
    public R getProcInfoList(@RequestParam Map<String, Object> params) {
        Set<String> procNameList = bdpDataCategoryService.getProcNameList(params);
        return R.okWithData(procNameList);
    }



    @ApiOperation(value = "通过数据类目和数据库查询数据表")
    @GetMapping("/getTableByCategoryIdAndDb")
    public R getTableByCategoryIdAndDb(@RequestParam("categoryId") Integer categoryId, @RequestParam("dbName") String dbName) {
        List<Map> tableList = bdpDataCategoryService.getTableByCategoryIdAndDb(categoryId,dbName);
        return R.okWithData(tableList);
    }



    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}